package com.douyuehan.doubao.wechatpay.controller;

import com.douyuehan.doubao.wechatpay.model.dto.WechatParam;
import com.douyuehan.doubao.wechatpay.model.vo.MessageTo;
import com.douyuehan.doubao.wechatpay.model.vo.WechatJSConfigTo;
import com.douyuehan.doubao.wechatpay.model.vo.WechatUserTo;
import com.douyuehan.doubao.wechatpay.model.vo.WxPaymentTo;
import com.douyuehan.doubao.wechatpay.service.WechatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

@RequestMapping(value = "api/v1/wechat")
@RestController
public class WechatController {

    private final static Logger logger = LoggerFactory.getLogger(WechatController.class);

    @Resource
    WechatService wechatService;

    @GetMapping(value = "/wx/jsConfig/{url}")
    @ResponseBody
    public WechatJSConfigTo getJsConfig(@PathVariable String url) {
        return wechatService.getJSConfig(url);
    }

    @GetMapping(value = "getOpenId/{code}")
    @ResponseBody
    public MessageTo<WechatUserTo> getOpenId(@PathVariable String code) {
        return wechatService.getOpenId(code);
    }

    @PostMapping("/wx/pay")
    @ResponseBody
    public MessageTo<WxPaymentTo> wxPay(@RequestBody WechatParam param, HttpServletRequest request) {
        return wechatService.wxPay(param, request);
    }

    @RequestMapping(value = "wechat_jsapi_notify")
    public String wechatNotify(HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        logger.debug("-----------------------客户微信回调了---------------------------");
        return wechatService.wechatNotify(request, response);
    }
}
